package clientServlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;

import controller.Controller;

@SuppressWarnings("serial")
public class OrderSettingClient extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("############################");
		try{
			int orderID = Integer.parseInt(req.getParameter("orderNum"));
			String userNameBiz = req.getParameter("userNameBiz");
			
			System.out.println("#orderID "+orderID);
			
			String statusNotification = req.getParameter("statusNotifications");
			String messageNotification = req.getParameter("messageNotifications");
			
			System.out.println("statusNotification" + statusNotification);
			System.out.println("messageNotification" + messageNotification);
			
			boolean statusNotificationBoolean = false;
			boolean messageNotificationBoolean = false;
			
			if (statusNotification!=null && statusNotification.equals("on"))
				statusNotificationBoolean = true;
			
			if (messageNotification!=null && messageNotification.equals("on"))
				messageNotificationBoolean = true;
			
			int result = Controller.getInstance().orderSettingClient(userNameBiz, orderID,statusNotificationBoolean,messageNotificationBoolean);
			
			
			switch(result)
			{
			case 0:
				req.getSession().removeAttribute("validErrorClientNewOrder");
				resp.sendRedirect("/allClientOrders.jsp");
				break;
			case 1:
				req.getSession().setAttribute("validErrorClientNewOrder","order not exist");	
				resp.sendRedirect("/allClientOrders.jsp");
				break;
			}
			
			
		}
		catch(NumberFormatException e)
		{
			System.out.println("number not format "+req.getParameter("orderID"));
			req.getSession().setAttribute("validErrorClientNewOrder","order id is not a number");	
			resp.sendRedirect("/allClientOrders.jsp");
			
		}
		

	}
}
